﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;

namespace Warensoft.PythonExtension
{
    /// <summary>
    /// 动态Python文件加载器
    /// </summary>
    public class DynamicIpyFileLoader:IpyLoaderBase 
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public DynamicIpyFileLoader()
        {

        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <remarks>会用该重载形式，会导致Python脚本被直接加载！</remarks>
        /// <param name="fileName">python文件名</param>
        public DynamicIpyFileLoader(string fileName)
        {
            this.FileName = fileName;
            this.Load();
        }
        /// <summary>
        /// Python脚本的路径
        /// </summary>
        public string FileName
        {
            get;
            set;
        }
        /// <summary>
        /// Python对象
        /// </summary>
        public dynamic  CallbackObject
        {
            get;
            set;
        }
        /// <summary>
        /// 加载指定的脚本文件
        /// </summary>
        public override  void Load()
        {
            if (this.FileName ==null||this.FileName=="")
            {
                throw new Exception("请指定脚本的路径！");
            }
            var runtime = Python.CreateRuntime();
            dynamic script = runtime.UseFile(this.FileName );
            this.CallbackObject = script;
            this.OnScriptLoaded();
        }
    }
}
